/**
 * 
 */
var xmlDoc = null, xmlhttp = null;
var xmlFile = "javascript/xmlpie.xml?";
var id;
function LoadXML(obj) {
	id = obj.id;
	xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest()
			: new ActiveXObject("Microsoft.XMLHttp");
	if (xmlhttp == null) {
		alert("你的浏览器不支持 XMLHttpRequest");
		return;
	}
	xmlhttp.open("GET", xmlFile, true);
	xmlhttp.setRequestHeader("Content-Type", "text/xml");
	
	switch(id){
	case "pie":{
		xmlhttp.onreadystatechange = ReadXMLpie;
		break;
	}
	case "bar":{
		xmlhttp.onreadystatechange = ReadXMLbar;
		break;
	}
	}
	xmlhttp.send(null);
}

function UnitExist(unitname) {
	var bool = false;
	var t = document.getElementById(unitname);
	if (t != null) {
		bool = true;
	}
	return bool;
}

function ReadXMLbar() {
	try {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var xmlDoc = xmlhttp.responseXML.documentElement;
			if (xmlDoc == null) {
				alert("返回的数据不正确。");
				return;
			}
			var chartType = id;
			var chartname;
			var chartnametag = xmlDoc.getElementsByTagName("chartname")[0];
			if (typeof (chartnametag) == "undefined")
				chartname = "";
			else
				chartname = chartnametag.childNodes[0].nodeValue;

			var unitList = xmlDoc.getElementsByTagName("unit");

			for (var i = 0; i < unitList.length; i++) {
				var unitname = unitList[i].attributes[0].value;
				if (UnitExist(unitname)) {
					var dataArray = new Array();
					dataArray.push(chartname);
					var dataSets = unitList[i].getElementsByTagName("sets");
					for (var ij = 0; ij < dataSets.length; ij++) {
						var dataSet = [];
						var dataList = dataSets[ij]
								.getElementsByTagName("data");
						for (var j = 0; j < dataList.length; j++) {
							var name = dataList[j].getElementsByTagName("name")[0].childNodes[0].nodeValue;
							var value = dataList[j]
									.getElementsByTagName("value")[0].childNodes[0].nodeValue;
							var dataRow = {};
							dataRow["value"] = value;
							dataRow["name"] = name;
							dataSet.push(dataRow);
						}
						dataArray.push(dataSet);
					}
					DrawDiv(chartType, unitname, dataArray);
				} else {
					continue;
				}
			}
		}
	} catch (e) {
		alert(e.message);
	}
}
function ReadXMLpie() {
	try {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var xmlDoc = xmlhttp.responseXML.documentElement;
			if (xmlDoc == null) {
				alert("返回的数据不正确。");
				return;
			}
			var chartType = id;
			var chartname;
			var chartnametag = xmlDoc.getElementsByTagName("chartname")[0];
			if (typeof (chartnametag) == "undefined")
				chartname = "";
			else
				chartname = chartnametag.childNodes[0].nodeValue;

			var unitList = xmlDoc.getElementsByTagName("unit");

			for (var i = 0; i < unitList.length; i++) {
				var unitname = unitList[i].attributes[0].value;
				if (UnitExist(unitname)) {
					var dataArray = new Array();
					dataArray.push(chartname);
					var dataSets = unitList[i].getElementsByTagName("sets");
					for (var ij = 0; ij < dataSets.length; ij++) {
						var dataSet = [];
						var dataList = dataSets[ij]
								.getElementsByTagName("data");
						for (var j = 0; j < dataList.length; j++) {
							var name = dataList[j].getElementsByTagName("name")[0].childNodes[0].nodeValue;
							var value = dataList[j]
									.getElementsByTagName("value")[0].childNodes[0].nodeValue;
							var dataRow = {};
							dataRow["value"] = value;
							dataRow["name"] = name;
							dataSet.push(dataRow);
						}
						dataArray.push(dataSet);
					}
					DrawDiv(chartType, unitname, dataArray);
				} else {
					continue;
				}
			}
		}
	} catch (e) {
		alert(e.message);
	}
}